<template>
  <div class="app-container">
    <eHeader :query="query"/>
    <!--表格渲染-->
    <el-table
      v-loading="loading"
      ref="treeTable"
      :data="data"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :select-on-indeterminate="true"
      row-key="id"
      size="small"
      border
      default-expand-all
      highlight-current-row>
      <el-table-column prop="id" align="center" label="id"/>
      <el-table-column prop="name" align="center" label="任务名称"/>
      <el-table-column prop="args" align="center" label="参数"/>
      <el-table-column prop="next_run_time" align="center" label="下一次运行时间"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <edit v-if="checkPermission(['admin','menu_all','menu_edit'])" :data="scope.row" :sup_this="sup_this"/>
          <el-popover
            v-if="checkPermission(['admin','menu_all','menu_delete'])"
            :ref="'startTask'"
            placement="top"
            width="200">
            <p>确定运行吗！</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs['startTask'].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="taskStart(scope.row.id, scope.row.name)">确定</el-button>
            </div>
            <el-button slot="reference" type="primary" style="margin-bottom:5px; margin-right: 3px" size="mini">运行</el-button>
          </el-popover>
          <el-popover
            v-if="checkPermission(['admin','menu_all','menu_delete'])"
            :ref="'stopTask'"
            placement="top"
            width="200">
            <p>确定停止吗！</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs['stopTask'].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="taskStop(scope.row.id, scope.row.name)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini" style="margin-bottom:5px; margin-right: 3px">停止</el-button>
          </el-popover>
          <el-popover
            v-if="checkPermission(['admin','menu_all','menu_delete'])"
            :ref="scope.row.id"
            placement="top"
            width="200">
            <p>确定删除吗！</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"></el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!-- </tree-table> -->
    <el-pagination
      :total="total"
      :page-sizes="[100, 200, 300, 400]"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import treeTable from '@/components/TreeTable'
import initData from '@/mixins/initData'
import { del, stop, start } from '@/api/tasks'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
export default {
  name: 'Tasks',
  components: { eHeader, edit, treeTable },
  mixins: [initData],
  data() {
    return {
      columns: [
        {
          text: '名称',
          value: 'name'
        }
      ],
      delLoading: false, sup_this: this, menus: []
    }
  },
  created() {
    this.$nextTick(() => {
      this.init(
        this.size = 10
      )
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = 'api/tasks/list/'
      return true
    },
    taskStop(id, name) {
      this.loading = true
      const params = { 'task_name': id }
      stop(params).then(res => {
        this.loading = false
        this.$refs['stopTask'].doClose()
        this.init()
        this.$message({
          showClose: true,
          type: 'success',
          message: '停止任务' + name + '成功!',
          duration: 2500
        })
      }).catch(err => {
        this.loading = false
        this.$refs['stopTask'].doClose()
        console.log(err)
      })
    },
    taskStart(id, name) {
      this.loading = true
      const params = { 'task_name': id }
      start(params).then(res => {
        this.loading = false
        this.$refs['startTask'].doClose()
        this.init()
        this.$message({
          showClose: true,
          type: 'success',
          message: '运行任务' + name + '成功!',
          duration: 2500
        })
      }).catch(err => {
        this.loading = false
        this.$refs['startTask'].doClose()
        console.log(err)
      })
    },
    subDelete(id) {
      this.delLoading = true
      const params = { 'task_name': id }
      del(params).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$message({
          showClose: true,
          type: 'success',
          message: '删除成功!',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>

</style>
